﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Linq;

namespace ClubWebsite
{
    public partial class PointsTable : System.Web.UI.UserControl
    {
        [Serializable()]
        public class TeamInfo
        {
            public TeamInfo(Guid id, string name, int displayOrder)
            {
                ID = id;
                Name = name;
                DisplayOrder = displayOrder;
            }
            public Guid ID;
            public string Name;
            public int DisplayOrder;
        }

        private TeamInfo[] _teamInfos;
        public TeamInfo[] TeamInfos
        {
            set
            {
                _teamInfos = value;
            }
        }

        public enum DisplayMode
        {
            Normal,
            Narrow
        }
        private DisplayMode _displayMode = DisplayMode.Normal;
        public DisplayMode Mode
        {
            set
            {
                _displayMode = value;
            }
        }

        private bool _ignorePostbackCheck = false;
        public bool IgnorePostbackCheck
        {
            set
            {
                _ignorePostbackCheck = value;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            Page.RegisterRequiresControlState(this);
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if ((!IsPostBack) || (_ignorePostbackCheck))
            {
                using (MainDataClassesDataContext databaseDataContext = Functions.GetDatabaseDataContext())
                {
                    Guid[] teamIDs = _teamInfos
                        .Select(ti => ti.ID)
                        .ToArray();
                    var leaguesTeams = databaseDataContext
                           .LeaguesTeams
                           .Where(lt => teamIDs.Contains(lt.TeamID))
                           .OrderBy(lt => lt.DisplayOrder);
                    var teams = _teamInfos
                        .Join(leaguesTeams, ti => ti.ID, lt => lt.TeamID, (ti, lt) => new { Description = ti.Name, DispayOrder = ti.DisplayOrder, LeaguesTeam = lt })
                        .OrderBy(ti => ti.DispayOrder);

                    // Display a list of matches
                    if (leaguesTeams.Count() > 0)
                    {
                        StringBuilder list = new StringBuilder();
                        foreach (var team in teams)
                        {
                            // Prepare the row
                            StringBuilder lineHTML = new StringBuilder();
                            lineHTML.Append(String.Format("<tr><td class=\"TeamName\">{0}</td>", team.Description));

                            // Rank
                            lineHTML.Append("<td>");
                            if (team.LeaguesTeam.Rank == null)
                            {
                                lineHTML.Append("N/A");
                            }
                            else
                            {
                                lineHTML.Append(team.LeaguesTeam.Rank);
                            }
                            lineHTML.Append("</td>");

                            // Matches played
                            lineHTML.Append("<td>");
                            if (team.LeaguesTeam.MatchesPlayed == null)
                            {
                                lineHTML.Append("0");
                            }
                            else
                            {
                                lineHTML.Append(team.LeaguesTeam.MatchesPlayed.ToString());
                            }
                            lineHTML.Append("</td>");

                            if (_displayMode == DisplayMode.Normal)
                            {
                                // Matches won
                                lineHTML.Append("<td>");
                                if (team.LeaguesTeam.MatchesWon == null)
                                {
                                    lineHTML.Append("0");
                                }
                                else
                                {
                                    lineHTML.Append(team.LeaguesTeam.MatchesWon.ToString());
                                }
                                lineHTML.Append("</td>");

                                // Matches drawn
                                lineHTML.Append("<td>");
                                if (team.LeaguesTeam.MatchesDrawn == null)
                                {
                                    lineHTML.Append("0");
                                }
                                else
                                {
                                    lineHTML.Append(team.LeaguesTeam.MatchesDrawn.ToString());
                                }
                                lineHTML.Append("</td>");

                                // Matches lost
                                lineHTML.Append("<td>");
                                if (team.LeaguesTeam.MatchesLost == null)
                                {
                                    lineHTML.Append("0");
                                }
                                else
                                {
                                    lineHTML.Append(team.LeaguesTeam.MatchesLost.ToString());
                                }
                                lineHTML.Append("</td>");
                            }

                            // Points
                            lineHTML.Append("<td>");
                            if (team.LeaguesTeam.Points == null)
                            {
                                lineHTML.Append("0");
                            }
                            else
                            {
                                lineHTML.Append(team.LeaguesTeam.Points.ToString());
                            }
                            lineHTML.Append("</td>");

                            if (_displayMode == DisplayMode.Normal)
                            {
                                // Score for
                                lineHTML.Append("<td>");
                                if (team.LeaguesTeam.ScoreFor == null)
                                {
                                    lineHTML.Append("0");
                                }
                                else
                                {
                                    lineHTML.Append(team.LeaguesTeam.ScoreFor.ToString());
                                }
                                lineHTML.Append("</td>");

                                // Score against
                                lineHTML.Append("<td>");
                                if (team.LeaguesTeam.ScoreAgainst == null)
                                {
                                    lineHTML.Append("0");
                                }
                                else
                                {
                                    lineHTML.Append(team.LeaguesTeam.ScoreAgainst.ToString());
                                }
                                lineHTML.Append("</td>");

                                // Score difference
                                lineHTML.Append("<td>");
                                if (team.LeaguesTeam.ScoreDifference == null)
                                {
                                    lineHTML.Append("0");
                                }
                                else
                                {
                                    lineHTML.Append(team.LeaguesTeam.ScoreDifference.ToString());
                                }
                                lineHTML.Append("</td>");
                            }
                            else
                            {
                                // Show the narrow table header
                                PointsTableNarrow.Visible = true;
                                PointsTableNoTeamSpecifiedHeader.Visible = false;
                            }

                            // End the row
                            list.Append(lineHTML.ToString() + "</tr>\r\n");
                        }
                        litList.Text = list.ToString();
                    }
                    else
                    {
                        //litNoList.Visible = true;
                        //MatchListNoTeamSpecifiedHeader.Visible = false;
                    }

                    // Show the correct table header
                    /*if (_teamID != Guid.Empty)
                    {
                        MatchListNoTeamSpecifiedHeader.Visible = false;
                        MatchListTeamSpecifiedHeader.Visible = true;
                    }*/
                }
            }
        }

        [Serializable()]
        private struct Properties
        {
            public TeamInfo[] TeamInfos;
            public DisplayMode DisplayMode;
            public Properties(TeamInfo[] teamInfos, DisplayMode displayMode)
            {
                TeamInfos = teamInfos;
                DisplayMode = displayMode;
            }
        }

        protected override object SaveControlState()
        {
            return new Properties(_teamInfos, _displayMode);
        }

        protected override void LoadControlState(object state)
        {
            if (state is Properties)
            {
                _teamInfos = ((Properties)state).TeamInfos;
                _displayMode = ((Properties)state).DisplayMode;
            }
        }
    }
}